package com.example.shiwuyouproject.ui.modelview

import androidx.lifecycle.MutableLiveData
import com.example.shiwuyouproject.base.BaseViewModel
import com.example.shiwuyouproject.utils.SPUtils
import com.example.shiwuyouproject.utils.SpConstant
import com.google.gson.Gson

class AccountBindingViewModel:BaseViewModel() {
    private val loginRepository by lazy { RetrofitClient.getApiService() }
    val myCodeState = MutableLiveData<Boolean>()
    /**
     * 获取验证码
     */
    fun smsSend(mobile:String){
        launch(
            block = {
                val codeStatus = loginRepository.smsSend(mobile,"wxbind")
                myCodeState.value=codeStatus.success()
                messageStatus.value=codeStatus.message()
            },
            error = {
                myCodeState.value = false
            },
            cancel = {

            },
            showErrorToast = false
        )
    }

    val userLoginState = MutableLiveData<Boolean>()

    /**
     * 登录
     */
    fun userLogin(mobile: String, captcha: String,wx_open_id:String,wx_uniq_id:String) {
        launch(
            block = {
                val codeStatus = loginRepository.getUserMobileLogin(mobile, captcha, wx_open_id,wx_uniq_id)
                if (!codeStatus.success()) {
                    messageStatus.value = codeStatus.message()
                } else {
                    SPUtils.getInstance()
                        .put(SpConstant.USER_INFO, Gson().toJson(codeStatus.result().userinfo()))
                    userLoginState.value = codeStatus.success()
                }
            },
            error = {
                userLoginState.value = false
                messageStatus.value = "网络错误，请重试！"
            },
            cancel = {

            },
            showErrorToast = false
        )
    }

}